package bitch;
import java.sql.Timestamp;
import java.util.Hashtable;

//This class is used to keep track of which appointments a user is looking at.
//This is necessary because we want live notification when an appointment is added/changed/removed,
//but we do not want to send everything to everyone.
//Each connected client(user) is associated with an instance of this class. 
public class Access
	{
	//The personID for this access
	private int personID;
	//If a user is looking at the appointments of some user in a time interval, these maps will contain that information.
	private Timestamp fromTime;
	private Timestamp toTime;
	private Hashtable<Integer,Boolean> access;
	//If a user is looking at a specific appointment, these variables will contain that information.
	//Otherwise they are -1;
	//It is not possible to look at multiple single appointments simultaneously.
	private int specificOwnerID;
	private int specificAppointmentID;
	public Access(int personID)
		{
		this.personID=personID;
		access = new Hashtable<Integer,Boolean>();
		fromTime = 	null;
		toTime = 	null;
		specificOwnerID=-1;
		specificAppointmentID=-1;
		}
	public int getPersonID()
		{
		return personID;
		}
	public void accessRange(Timestamp from,Timestamp to)
		{
		fromTime=from;
		toTime=to;
		}
	public void accessPerson(int personID)
		{
		access.put(personID, true);
		}
	public void accessSpecific(int ownerID,int appointmentID)
		{
		specificOwnerID=ownerID;
		specificAppointmentID=appointmentID;
		}
	public void unsetPerson(int personID)
		{
		access.remove(personID);
		}
	public boolean hasSpecificAccess(int ownerID,int appointmentID)//set to -1,-1 for no specific access
		{
		return (specificOwnerID==ownerID && specificAppointmentID==appointmentID);
		}
	public int[] getPersons()
		{
		Object[] temp = access.keySet().toArray();
		int[] result = new int[temp.length];
		for(int i=0; i<result.length; i++)
			{
			result[i]=(Integer)temp[i];
			}
		return result;
		}
	public Timestamp getFromTime()
		{
		return fromTime;
		}
	public Timestamp getToTime()
		{
		return toTime;
		}
	public boolean isAccessingPerson(int personID)
		{
		Boolean b = access.get(personID);
		if (b==null)
			return false;
		return b;
		}
	}
